//index.js
//获取应用实例
var app = getApp()
Page({
  data: {
    frate: 0,
    fratebak: 0,
    socket_tag: null,
    platforms: [],
  },
  //事件处理函数
  getMarket: function () {
    var that = this;
    wx.request({
      url: 'https://lhang.com/kline/klineList',
      data: {
        xcx: 'wuyu'
      },
      method: 'GET',
      header: {
        'Content-Type': 'application/json'
      },
      success: function (res) {
        try {
          var list = res.data.dataWrapper.dailyDatas
          var r_list = [];
          list.forEach((item, i) => {
            that.setData({ 'frate': list[list.length-1].frate })
            if (i < (list.length-1)) {
              var el = JSON.parse(item.dailyData);
              el.highPrice = el.highPrice.toFixed(2);
              el.lowPrice = el.lowPrice.toFixed(2);
              el.vol = el.vol.toFixed(4);
              el.closingPrice = el.closingPrice.toFixed(2);
              el.changePercent = el.changePercent.toFixed(2);
              el.closingPrice_m = (el.closingPrice/that.data.frate).toFixed(2);
              r_list.push(el);
            }
          })
          that.setData({ 'platforms': r_list })
        } catch (e) {
          //TODO handle the exception
        }
      }
    })
  },
  onPullDownRefresh: function () {
    this.getMarket();
    wx.stopPullDownRefresh()
  },
  onShareAppMessage: function () {
    return {
      title: 'ZEC/CNY 行情',
      desc: '流通价值 链接你我',
      // path: '/pages/index/index'
    }
  },
  onLoad: function () {
  },
  onUnload: function () {
    // 页面关闭
    clearInterval(this.socket_tag);
  },
  onShow: function () {
    // 页面显示
    var that = this;
    that.getMarket();
    this.socket_tag = setInterval(function () {
      that.getMarket();
    }, 2000)
  },
  onHide: function () {
    // 页面隐藏
    clearInterval(this.socket_tag);
  }
})